package ecomp.uefs.micc.sharedbox.model;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.swing.JDialog;
import javax.swing.JOptionPane;

import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.commons.mail.SimpleEmail;

public class Server {

	protected static ServerSocket server;
	private static int port = 12345;
	private boolean state = false;

	private List<User> USERS_LIST;

	public Server() {

	}

	/**
	 * 
	 */

	public void start() {
		// TODO Auto-generated method stub
		try {

			server = new ServerSocket(port);
			JOptionPane.showMessageDialog(null, "Server listening on port "
					+ port, "Server Runing", 1);

			while (getState()) {

				Socket client = server.accept();
				System.out.println("Nova conexão com o cliente " +   
				         client.getInetAddress().getHostAddress()
				       );

				new OtherClass(client).start();
				/*
				 * JOptionPane.showMessageDialog(null,
				 * "New Conection With Client" +
				 * client.getInetAddress().getHostAddress(),"Server Runing", 1);
				 */
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void stop() {
		try {
			server.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public boolean getState() {
		// TODO Auto-generated method stub
		return state;
	}

	public void setState(boolean state) {
		this.state = state;
	}

	public int getPort() {
		return port;
	}

	public static void listenClient() {

	}

	public boolean authentication(Client client) {
		boolean result = false;

		for (User user : this.USERS_LIST) {
			if (client.getUser().getUsername() == user.getUsername()
					&& client.getUser().getPassword() == user.getPassword()) {
				result = true;
			} else {
				result = false;
			}
		}
		return result;

	}

	public void sendEmail() throws EmailException {

		Properties props = new Properties();
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.socketFactory.port", "465");
		props.put("mail.smtp.socketFactory.class",
				"javax.net.ssl.SSLSocketFactory");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.port", "465");

		Session session = Session.getDefaultInstance(props,
				new javax.mail.Authenticator() {
					@SuppressWarnings("deprecation")
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication("emanuel.uefs@gmail.com","");
					}
				});

		/** Ativa Debug para sessão */
		session.setDebug(true);
		try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("emanuel.uefs@gmail.com")); //Remetente

            Address[] toUser = InternetAddress //Destinatário(s)
                       .parse("ejl.ecomp@gmail.com, ejl.ecomp@hotmail.com");  

            message.setRecipients(Message.RecipientType.TO, toUser);
            message.setSubject("Enviando email com JavaMail");//Assunto
            message.setText("Enviei este email utilizando JavaMail com minha conta GMail!");
            /**Método para enviar a mensagem criada*/
            Transport.send(message);

            System.out.println("Feito!!!");

       } catch (MessagingException e) {
            throw new RuntimeException(e);
      }

	}
	
	private void addUser(Client client){		
		User user = client.getUser();
		this.USERS_LIST.add(user);
	}

}
